package com.gs.synedocket.util;

import android.os.CountDownTimer;

public class CountDownTimerUtil {
    private CountDownTimer timer;
    private long countDown;
    private OnResponseListener onResponseListener;

    public CountDownTimerUtil(long countDown,OnResponseListener onResponseListener) {
        this.countDown = countDown;
        this.onResponseListener = onResponseListener;
        startCountDown();
    }

    public void startCountDown() {
        if (timer == null) {
            timer = new CountDownTimer(countDown, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    onResponseListener.onTick(millisUntilFinished);
                }

                @Override
                public void onFinish() {
                    onResponseListener.onFinish();
                }
            };
        }
        timer.start();
    }

    public void cancelCountDown() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public interface OnResponseListener {
        void onTick(long millisUntilFinished);

        void onFinish();
    }
}
